package com.example.demo.mapper;

import com.example.demo.entity.CourseQuestion;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CourseQuestionMapper {
    // 新增课程问题
    @Insert("INSERT INTO course_questions (student_id, course_id, question) VALUES (#{student_id}, #{course_id}, #{question})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertCourseQuestion(CourseQuestion courseQuestion);

    // 删除课程问题
    @Delete("DELETE FROM course_questions WHERE id = #{id}")
    int deleteCourseQuestionById(int id);

    // 修改课程问题
    @Update("UPDATE course_questions SET student_id = #{student_id}, course_id = #{course_id}, question = #{question}, teacher_id = #{teacher_id}, answer = #{answer}, answered_at = #{answered_at} WHERE id = #{id}")
    int updateCourseQuestion(CourseQuestion courseQuestion);

    // 查询课程问题
    @Select("SELECT * FROM course_questions WHERE id = #{id}")
    CourseQuestion selectCourseQuestionById(int id);

    // 查询所有课程问题
    @Select("SELECT * FROM course_questions")
    List<CourseQuestion> selectAllCourseQuestions();

    // 联合查询课程的所有问题
    @Select("SELECT cq.* FROM course_questions cq JOIN courses c ON cq.course_id = c.id WHERE c.id = #{courseId}")
    List<CourseQuestion> selectCourseQuestionsByCourseId(int courseId);

    // 联合查询教师回答的所有问题
    @Select("SELECT cq.* FROM course_questions cq JOIN teachers t ON cq.teacher_id = t.id WHERE t.id = #{teacherId}")
    List<CourseQuestion> selectCourseQuestionsByTeacherId(int teacherId);
}